package com.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.entity.SysUser;
import com.crm.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/main")
    public String main(){
        return "main";
    }
    @PostMapping("/login")
    public String login(String usrName, String usrPassword, HttpSession session, Model model){
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("usr_name",usrName);
        qw.eq("usr_password",usrPassword);
        SysUser sysUser = sysUserService.getOne(qw);
        if (sysUser != null) {
            session.setAttribute("loginUser",sysUser);
            return "main";
        } else {
            model.addAttribute("msg","用户名或密码错误，登录失败！");
            return "forward:/login.jsp";
        }
    }
}
